<template>
  <div class="">

    <el-dialog :title="titleShow" :visible="isAddDept" @close="clearRults">
      <el-form ref="deptForm" :model="form" label-width="120px" :rules="rules">
        <el-form-item label="部门名称" prop="name">
          <el-input v-model="form.name" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input v-model="form.code" style="width:80%"></el-input>
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select v-model="form.manager" style="width:80%" placeholder="请选择" @focus="selectPrincipal ">
            <el-option v-for="val in peoples" :key="val.id" :label="val.username" :value="val.username"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input v-model="form.introduce" style="width:80%"></el-input>
        </el-form-item>
      </el-form>
      <el-row slot="footer" type="flex" justify="center">
        <!-- 列被分为24 -->
        <el-col :span="6">
          <el-button size="small" @click="clearRults">取 消</el-button>
          <el-button type="primary" size="small" @click="submitBtn">确 定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
import { getEmployeeSimple } from '@/api/employees.js'
import { inquireDepartment, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments.js'
export default {
  name: '',
  components: {},
  props: {
    isAddDept: {
      type: Boolean,
      default: false
    },
    node: {
      type: [Object, String],
      default: null
    }
  },
  data() {
    //   判断 某个部门下是否 已有相同名字
    const checkNameRepeat = async(rule, value, callback) => {
      // 结构出 部门信息
      const { depts } = await inquireDepartment()
      var isRepeat = false
      // 如果有id 排除自己
      if (this.form.id) {
        isRepeat = depts.filter(item => item.pid === this.node.id && item.id !== this.form.id).some(item => item.name === value)
      } else {
        // 没有id
        //  判断该部门下已有的 子部门 名称 和正在添加的字部门名称是否一致   返回true / false
        isRepeat = depts.filter(item => item.pid === this.node.id).some(item => item.name === value)
      }

      isRepeat ? callback(new Error('该部门下已有' + value + '部门了')) : callback()
    }
    // 判断 部门编码是否 有相同
    const checkCodeRepeat = async(rule, value, callback) => {
      const { depts } = await inquireDepartment()
      var isRepeat = false
      // 判断是否有id
      if (this.form.id) {
        // 有id 排除自己
        isRepeat = depts.filter(val => val.id !== this.form.id).some(item => item.code === value && value)
      }
      //  判断 部门编码是否一样 返回true / false
      isRepeat = depts.some(item => item.code === value && value)
      isRepeat ? callback(new Error('该部门下已有' + value + '部门了')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      },
      // 定义校验规则
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门名称要求1-50个字符', trigger: 'blur' }, {
            trigger: 'blur', validator: checkNameRepeat
          }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 1, max: 50, message: '部门编码要求1-50个字符', trigger: 'blur' },
          { trigger: 'blur', validator: checkCodeRepeat }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 1, max: 300, message: '部门介绍要求1-50个字符' }]
      },
      peoples: []
    }
  },
  computed: {
    titleShow() {
      return this.form.id ? '编辑部门' : '新增子部门'
    }
  },
  watch: {},
  created() { this.selectPrincipal() },
  methods: {
    // 获取 负责人的名称---------------------
    async selectPrincipal() {
      this.peoples = await getEmployeeSimple()
    },
    // 点击确认 提交 ------------------------
    submitBtn() {
      this.$refs.deptForm.validate(async isok => {
        if (isok) {
          // 判断是否有id
          if (this.form.id) {
            // 有id 保存修改 部门信息
            await updateDepartments({ ...this.form, pid: this.node.id })
          } else {
            // 验证成功 添加子部门
            await addDepartments({ ...this.form, pid: this.node.id })
          }

          // 更新视图
          this.$emit('addDept')
          this.$message.success('添加部门成功')
          // 关闭弹框
          this.$emit('update:isAddDept', false)
        }
      })
    },
    // 点击取消--------------------
    clearRults() {
      this.form = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门负责人
        introduce: '' // 部门介绍
      }
      // 关闭弹框
      this.$emit('update:isAddDept', false)
      // 取消校验规则
      this.$refs.deptForm.resetFields()
    },
    // 查询部门详情
    async  getDepartDetail(id) {
      this.form = await getDepartDetail(id)
    }
  }
}
</script>

<style lang="less" scoped>
</style>
